<?php
/**
 * Created by PhpStorm.
 * User: 轩
 * Date: 2017/9/4
 * Time: 9:08
 */

namespace app\home\controller\Cloud;

use think\Db;
use think\Controller;
use think\Session;
use app\home\service\AccountService;
use app\home\controller\Common;

class Addgoods extends Controller
{
    private $MINI_ID;
    private $WX_id;
    private $WX_secret;

    public function _initialize()
    {

        parent::_initialize();
        $A = new AccountService();
        $this->MINI_ID = Session::get('login_miniID');
        $this->WX_id = $A->getminiAppsData('appid', $this->MINI_ID);
        $this->WX_secret = $A->getminiAppsData('secret', $this->MINI_ID);
    }

    public function index()
    {

        $CONFIG = [
            'title' => '商品信息',
            'css' => [
                'home/css/plugins/clockpicker/clockpicker.css',
                'home/css/plugins/option/htools.select.skin.css',
                'companys/css/webuploader_imgs.css',
                'home/css/plugins/webuploader/webuploader.css'
            ],
            'js' => [
                'home/js/plugins/clockpicker/clockpicker.js',
                'home/js/plugins/option/jquery.htools.select.js',
                'home/js/plugins/webuploader/webuploader.js'
            ],
            'path' => [
                '添加商品'
            ],
        ];
        $common = new Common();
        $upload_Token = $common->getQiniuUploadToken()['data']['token'];
        $feature = Db::table('temp_cd_item')->where('miniapps_id', $this->MINI_ID)->select();
        $brand = Db::table('temp_cd_brand')->where('miniapps_id', $this->MINI_ID)->select();

        //查询banner图
        $banner = Db::table('temp_cd_shop_images')->where('miniapps_id',$this->MINI_ID)->where('shop_id',null)->where('type',1)->select();

        //查询详情图
        $detail = Db::table('temp_cd_shop_images')->where('miniapps_id',$this->MINI_ID)->where('shop_id',null)->where('type',2)->select();

        $DATA = [
            'token' => $upload_Token,
            'feature' => $feature ? $feature : [],
            'brand' => $brand ? $brand : [],
            'banner' => $banner ? $banner : [],
            'detail' => $detail ? $detail : []

        ];
        $this->assign('DATA', $DATA);
        $this->assign('CONFIG', $CONFIG);
        return $this->fetch('_Tpl/cloud/addgoods');
    }

    public function banner_pic()
    {
        if (!input('key')) {
            $arr['info'] = 0;
            return $arr;
        }
        $data = [
            'shop_id' => null,
            'url' => input('key'),
            'miniapps_id' => $this->MINI_ID,
            'add_time' => date('y-m-d H:i:s', time()),
            'type' => 1,
        ];

        $num = Db::table('temp_cd_shop_images')->where('miniapps_id',$this->MINI_ID)->where('shop_id',null)->where('type',1)->count();
        if ($num > 2) {
            $arr['info'] = 2;
            return $arr;
        }
        $res = Db::table('temp_cd_shop_images')->insert($data);

        if ($res) {
            $arr['info'] = 1;
            return $arr;
        } else {
            $arr['info'] = 0;
            return $arr;
        }

    }

    public function del_banner()
    {
        $data = [
            'url' => input('key'),
            'type' => 1,
            'miniapps_id'=>$this->MINI_ID
        ];
        $res = Db::table('temp_cd_shop_images')->where($data)->where('shop_id',null)->delete();
        if ($res) {
            $arr['info'] = 1;
            return $arr;
        } else {
            $arr['info'] = 0;
            return $arr;
        }
    }

    public function detail_pic()
    {
        if (!input('key')) {
            $arr['info'] = 0;
            return $arr;
        }
        $data = [
            'shop_id' => null,
            'url' => input('key'),
            'miniapps_id' => $this->MINI_ID,
            'add_time' => date('y-m-d H:i:s', time()),
            'type' => 2,
        ];

        $num = Db::table('temp_cd_shop_images')->where('miniapps_id',$this->MINI_ID)->where('shop_id',null)->where('type',2)->count();

        if ($num > 11) {
            $arr['info'] = 2;
            return $arr;
        }
        $res = Db::table('temp_cd_shop_images')->insert($data);

        if ($res) {
            $arr['info'] = 1;
            return $arr;
        } else {
            $arr['info'] = 0;
            return $arr;
        }
    }

    public function del_detail()
    {
        $data = [
            'url' => input('key'),
            'type' => 2,
            'miniapps_id'=>$this->MINI_ID
        ];
        $res = Db::table('temp_cd_shop_images')->where($data)->where('shop_id',null)->delete();
        if ($res) {
            $arr['info'] = 1;
            return $arr;
        } else {
            $arr['info'] = 0;
            return $arr;
        }
    }

    public function insert()
    {
        $data = [
            'item_id' => input('item_id'),
            'brand_id' => input('brand_id'),
            'title' => input('title'),
            'detail' => input('detail'),
            'xiangqing' => input('xiangqing'),
            'storage' => input('storage'),
            'original_cost' => input('original_cost'),
            'add_time' => date("Y-m-d H:i:s"),
            'miniapps_id' => $this->MINI_ID
        ];


        $shop_id = Db::table('temp_cd_shop')->insertGetId($data);
        $marque_price = input('post.marque_price/a');

        foreach($marque_price as $value){
            Db::table('temp_cd_price')->insert(['miniapps_id'=>$this->MINI_ID,'shop_id'=>$shop_id,'marque'=>$value['marque'],'price'=>$value['price']]);
        }
        $res = Db::table('temp_cd_shop_images')->where('miniapps_id',$this->MINI_ID)->where('shop_id',null)->update(['shop_id'=>$shop_id]);

        if($res){
            $arr['info']=1;
            return $arr;
        }else{
            $arr['info']=3;
            return $arr;
        }

    }


}

?>